package com.macbulldev.poc.testgin.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jxl.common.Logger;

import net.youngdev.util.Context;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.macbulldev.poc.testgin.service.WebappUtils;

@Controller
public class MiscController 
{
	private static final Logger LOGGER = Logger.getLogger(MiscController.class);
	@RequestMapping(value="/public/blank.img",method=RequestMethod.GET)
	public void setAppUrl(HttpServletRequest request, HttpServletResponse response)
	{
		ServletContext ctx = (ServletContext)Context.getInstance().getContext();
		if (ctx.getAttribute("APPLICATION_BASE_URL")==null)
		{
			StringBuffer url =request.getRequestURL();
			String urlBase=url.substring(0, url.indexOf(request.getRequestURI()));
			ctx.setAttribute("APPLICATION_BASE_URL",urlBase );
			LOGGER.debug(ctx.getAttribute("APPLICATION_BASE_URL"));
		}
		OutputStream os= null;
		InputStream is=null;
		try
		{
			os=response.getOutputStream();

		
		response.setContentType("image/gif");
		//LOGGER.info(contentType);
		String imageFilename = ctx.getRealPath("/images/clear.gif");
        is= new FileInputStream(new File(imageFilename));
        response.setContentLength(is.available());
        byte[] b = new byte[is.available()];

        is.read(b);
        
        os.write(b);
  		
		}
		catch (Exception e)
		{
			
		}
		finally
		{
			try
			{
				is.close();
			}
			catch (Exception e)
			{
				
			}
			try
			{
				os.close();
			}
			catch (Exception e) {
			}
		}
		
		
		
	}
	
	@RequestMapping(value={"/failed403"},method=RequestMethod.GET)
	public String render403Page(HttpServletRequest request,HttpSession session,Model model)
	{
		try
		{
		LOGGER.debug("-----> Denied Controller triggered: "+WebappUtils.getAuthenticationObject(session).getEmail());
		}
		catch (Exception e0){
			
		}
		return "denied";
	}

}
